#include "StdAfx.h"
#include "CBoardCell2D.h"

//#define WHITE RGB(255, 16, 255)
#define GRAY RGB(236, 233, 216)


BEGIN_MESSAGE_MAP(CBoardCell2D, CWnd)
    ON_WM_PAINT()
    ON_WM_CREATE()
    ON_WM_MOUSEMOVE()
    ON_WM_LBUTTONUP( )
END_MESSAGE_MAP()

CBoardCell2D::CBoardCell2D(void)
{
}

CBoardCell2D::CBoardCell2D(int nCellID, Color CellColor, int nWidth, int nHeight)
	:CBoardCell(nCellID, CellColor, nWidth, nHeight)
{
	m_initColor			= CellColor;
	m_hoverColor		= Color::Ivory;
	m_mouseClickColor	= Color::BlueViolet;
}

CBoardCell2D::~CBoardCell2D(void)
{
}

void CBoardCell2D::setInitColor( Color initColor )
{
}
void CBoardCell2D::setHoverColor( Color hoverColor )
{
}
void CBoardCell2D::setMouseClickColor( Color mouseClickColor )
{
}

Color CBoardCell2D::getInitColor(  )
{
	return m_initColor;
}
Color CBoardCell2D::getHoverColor(  )
{
	return m_hoverColor;
}
Color CBoardCell2D::getMouseClickColor(  )
{
	return m_mouseClickColor;
}


/*****************************************************************************
* ++
* Method name   :    int CBoardCell2D::OnCreate(LPCREATESTRUCT lpCreateStruct)
* Description   :    On event Create handler. This function will be called when the window is created
*
* Parameters    :  LPCREATESTRUCT lpCreateStruct (not use)
* Return values :    
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*                    
* --
*****************************************************************************/

int CBoardCell2D::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    Gdiplus::GdiplusStartupInput gdiplusStartupInput;
    Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

    return(0);
}

/*****************************************************************************
 * ++
 * Method name   :    void CBoardCell2D::OnPaint(void)
 * Description   :    On event Paint handler. This function will be called when the WM_PAIN is created
 *
 * Parameters    : 
 * Return values :    
 * Mod. History  :    03/12/2012 - BaoTD1 (first created)
 *
 * --
 *****************************************************************************/
void CBoardCell2D::OnPaint(void)
{
    CPaintDC dc(this); // Creates a device context for the client area, which
                   // also erases the area to be drawn.
    RECT rect;
//	rect = this->m_ClientRect;
	GetClientRect(&rect);

    HDC Memhdc;
    HBITMAP Membitmap;
    CRect MainRect ;//= this->m_ClientRect;;
	MainRect.left = rect.left;
	MainRect.bottom = rect.bottom;
	MainRect.top = rect.top;
	MainRect.right = rect.right;
	//GetClientRect(&MainRect);
//	HWND handle = GetSafeHwnd();
//	 ::GetWindowRect(handle,MainRect);
//	GetClientRect(MainRect);
   // CreateMainRect(&MainRect);
    // Double Buffer
    Memhdc = CreateCompatibleDC(dc.m_hDC);
    Membitmap = CreateCompatibleBitmap(dc.m_hDC, MainRect.Width() , MainRect.Height());
    CRect MemRect(MainRect.left,MainRect.top, MainRect.Width(), MainRect.Height());
    SelectObject(Memhdc, Membitmap);

    FillRect(Memhdc, MemRect, CreateSolidBrush(WHITE));
    Rectangle(Memhdc,MemRect.left, MemRect.top,MemRect.right, MemRect.bottom);
    // GDI+
    Graphics graphics(Memhdc);
    graphics.SetSmoothingMode(SmoothingModeHighQuality);

//	rect = this->m_ClientRect;
    // Draw Board 2D
    DrawButtonLeft(&graphics, rect);
  
    BitBlt(	dc,		MainRect.left, MainRect.top, MainRect.Width(), MainRect.Height(), 
			Memhdc,	MemRect.left , MemRect.top, SRCCOPY);
    DeleteObject(Membitmap);
    DeleteDC    (Memhdc);
    DeleteDC    (dc);
}

/******************************************************************************
* ++
* Method name   :    void CBoardCell2D::DrawBoard2D(Graphics* graphics)
* Description   :    Draw Board 2D.
*
* Parameters    :  "graphic"    [in,out] If non-null, the graphic.
* Return values :    true if it succeeds, false if it fails.
* 
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*
* --
*****************************************************************************/

BOOL CBoardCell2D::CreateBoardCell( const RECT& rect, CWnd* pParentWnd)
{
	int nID = 0;
	///* Create window to draw board */
     if (!CWnd::Create(	NULL, 
						NULL, 
						WS_VISIBLE, 
						rect, 
						pParentWnd, nID, 
						NULL))
    {
		
        throw LPCSTR("CTopViewImageControl::Create window fail !");
        return (FALSE);
    }
	this->m_ClientRect = rect;
	return TRUE;
}
/******************************************************************************
* ++
* Method name   :    void CBoardCell2D::DrawBoard2D(Graphics* graphics)
* Description   :    Draw Board 2D.
*
* Parameters    :  "graphic"    [in,out] If non-null, the graphic.
* Return values :    None
* 
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*
* --
*****************************************************************************/
void CBoardCell2D::OnMouseMove(UINT wParam, CPoint pos)
{
	//
}

/******************************************************************************
* ++
* Method name   :    void CBoardCell2D::DrawButtonLeft(Graphics* graphics)
* Description   :    Draw Button Left.
*
* Parameters    :  "graphic"    [in,out] If non-null, the graphic.
* Return values :   none
* 
* Mod. History  :    03/12/2012 - BaoTD1 (first created)
*
* --
*****************************************************************************/

void CBoardCell2D::DrawButtonLeft( Graphics* graphics,  RECT rect )
{
   /* Gdiplus::Font myFont(L"Arial", 8);
    StringFormat format;
    format.SetAlignment(StringAlignmentCenter);*/
    SolidBrush blackBrush(Color(255, 0, 0, 0));
   
	// PenGray
	Pen PenGray(Color::Blue);

	SolidBrush ColorBrush(	Color::Blue );

	// RECT trect;
	//GetClientRect(&trect);
	// RECT rectTemp = this->m_ClientRect;
	 
	//		// GetClientRect(&rect);
	//rect.bottom = trect.bottom;
	//rect
	//		 //GetWowRect(&rect);
			 
	//HWND handle = GetSafeHwnd();
	// ::GetWindowRect(handle,rect);
		//	GetClientRect(rect);
			
			//Rect r;
			//GetClientRect ( &r );
			//r.
			//PenGray.SetColor(Color::Aqua);
			graphics->FillRectangle(	&ColorBrush,
										rect.left,
										rect.right,
										rect.top - rect.bottom,
										rect.right - rect.left );
			
			//graphics->FillRectangle ( &ColorBrush, &rect );
			
//graphics->FillRectangle(		
}

